<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>金山一面复习 | ShuangChenYue</title>
    <meta name="generator" content="VuePress 1.9.10">
    <link rel="icon" href="https://cdn.jsdelivr.net/gh/cmty256/imgs-blog@main/logo/白云.38zbldnhh180.jpg">
    <meta name="description" content="满招损，谦受益">
    <meta name="keywords" content="专注于Cpp语言的旅行者">
    
    <link rel="preload" href="/assets/css/0.styles.952d6952.css" as="style"><link rel="preload" href="/assets/js/app.67adcfd9.js" as="script"><link rel="preload" href="/assets/js/4.9aaa1650.js" as="script"><link rel="preload" href="/assets/js/1.5474518c.js" as="script"><link rel="preload" href="/assets/js/3.593d14fc.js" as="script"><link rel="preload" href="/assets/js/192.712a164e.js" as="script"><link rel="prefetch" href="/assets/js/10.3242746b.js"><link rel="prefetch" href="/assets/js/100.9224de43.js"><link rel="prefetch" href="/assets/js/101.f0d1b059.js"><link rel="prefetch" href="/assets/js/102.996bfc6d.js"><link rel="prefetch" href="/assets/js/103.9bfdbd6f.js"><link rel="prefetch" href="/assets/js/104.8613f283.js"><link rel="prefetch" href="/assets/js/105.aa6e809e.js"><link rel="prefetch" href="/assets/js/106.90192392.js"><link rel="prefetch" href="/assets/js/107.e82a40b7.js"><link rel="prefetch" href="/assets/js/108.994cd438.js"><link rel="prefetch" href="/assets/js/109.ec15acc2.js"><link rel="prefetch" href="/assets/js/11.c04b41c1.js"><link rel="prefetch" href="/assets/js/110.c32d8576.js"><link rel="prefetch" href="/assets/js/111.453b5d50.js"><link rel="prefetch" href="/assets/js/112.ffbdb3a4.js"><link rel="prefetch" href="/assets/js/113.12b8ad7d.js"><link rel="prefetch" href="/assets/js/114.899d2998.js"><link rel="prefetch" href="/assets/js/115.b7ad9576.js"><link rel="prefetch" href="/assets/js/116.a8394748.js"><link rel="prefetch" href="/assets/js/117.0edfe25b.js"><link rel="prefetch" href="/assets/js/118.9161b1fe.js"><link rel="prefetch" href="/assets/js/119.be59e21b.js"><link rel="prefetch" href="/assets/js/12.41437bf6.js"><link rel="prefetch" href="/assets/js/120.bcf439fb.js"><link rel="prefetch" href="/assets/js/121.c3d251b8.js"><link rel="prefetch" href="/assets/js/122.62b1caba.js"><link rel="prefetch" href="/assets/js/123.787c2ab0.js"><link rel="prefetch" href="/assets/js/124.a880746f.js"><link rel="prefetch" href="/assets/js/125.d8edfe7b.js"><link rel="prefetch" href="/assets/js/126.4ff01546.js"><link rel="prefetch" href="/assets/js/127.9416d1ff.js"><link rel="prefetch" href="/assets/js/128.01a4a7a0.js"><link rel="prefetch" href="/assets/js/129.76876665.js"><link rel="prefetch" href="/assets/js/13.922328e9.js"><link rel="prefetch" href="/assets/js/130.7f631dd9.js"><link rel="prefetch" href="/assets/js/131.c9e0fde9.js"><link rel="prefetch" href="/assets/js/132.1c04cde5.js"><link rel="prefetch" href="/assets/js/133.e8f381cd.js"><link rel="prefetch" href="/assets/js/134.03d19f8b.js"><link rel="prefetch" href="/assets/js/135.44607494.js"><link rel="prefetch" href="/assets/js/136.6a1eb3c9.js"><link rel="prefetch" href="/assets/js/137.27898fd1.js"><link rel="prefetch" href="/assets/js/138.5bc0cf54.js"><link rel="prefetch" href="/assets/js/139.c2d1addc.js"><link rel="prefetch" href="/assets/js/14.e54d7526.js"><link rel="prefetch" href="/assets/js/140.052ec8e4.js"><link rel="prefetch" href="/assets/js/141.131abb5a.js"><link rel="prefetch" href="/assets/js/142.6ba6c07b.js"><link rel="prefetch" href="/assets/js/143.5dd51d22.js"><link rel="prefetch" href="/assets/js/144.b45afca8.js"><link rel="prefetch" href="/assets/js/145.faa9fb04.js"><link rel="prefetch" href="/assets/js/146.b54c024d.js"><link rel="prefetch" href="/assets/js/147.a1223242.js"><link rel="prefetch" href="/assets/js/148.4767bcb2.js"><link rel="prefetch" href="/assets/js/149.b65ab046.js"><link rel="prefetch" href="/assets/js/15.7082a3da.js"><link rel="prefetch" href="/assets/js/150.9bd8c175.js"><link rel="prefetch" href="/assets/js/151.9f830e96.js"><link rel="prefetch" href="/assets/js/152.41cde7f0.js"><link rel="prefetch" href="/assets/js/153.f57d65e0.js"><link rel="prefetch" href="/assets/js/154.5d7c8d51.js"><link rel="prefetch" href="/assets/js/155.0ae99532.js"><link rel="prefetch" href="/assets/js/156.5a54e043.js"><link rel="prefetch" href="/assets/js/157.c25b5d40.js"><link rel="prefetch" href="/assets/js/158.aa025b46.js"><link rel="prefetch" href="/assets/js/159.47939d88.js"><link rel="prefetch" href="/assets/js/16.fc775b7b.js"><link rel="prefetch" href="/assets/js/160.f8624459.js"><link rel="prefetch" href="/assets/js/161.7a075dc2.js"><link rel="prefetch" href="/assets/js/162.1d48f266.js"><link rel="prefetch" href="/assets/js/163.5d68a99f.js"><link rel="prefetch" href="/assets/js/164.1262d0e5.js"><link rel="prefetch" href="/assets/js/165.2ccf0bdd.js"><link rel="prefetch" href="/assets/js/166.21ece4d9.js"><link rel="prefetch" href="/assets/js/167.bf8adb95.js"><link rel="prefetch" href="/assets/js/168.1cb8440d.js"><link rel="prefetch" href="/assets/js/169.1dd1e396.js"><link rel="prefetch" href="/assets/js/17.ecc7be70.js"><link rel="prefetch" href="/assets/js/170.c29ec18f.js"><link rel="prefetch" href="/assets/js/171.38820827.js"><link rel="prefetch" href="/assets/js/172.bbc8ffc6.js"><link rel="prefetch" href="/assets/js/173.470e21e7.js"><link rel="prefetch" href="/assets/js/174.3c2df318.js"><link rel="prefetch" href="/assets/js/175.d2690cdb.js"><link rel="prefetch" href="/assets/js/176.9ca64696.js"><link rel="prefetch" href="/assets/js/177.76f3271d.js"><link rel="prefetch" href="/assets/js/178.d7d9def2.js"><link rel="prefetch" href="/assets/js/179.b5644743.js"><link rel="prefetch" href="/assets/js/18.31fe7ecd.js"><link rel="prefetch" href="/assets/js/180.7592d5ef.js"><link rel="prefetch" href="/assets/js/181.5cb77d35.js"><link rel="prefetch" href="/assets/js/182.6fa5633c.js"><link rel="prefetch" href="/assets/js/183.b3a53d1b.js"><link rel="prefetch" href="/assets/js/184.3815c537.js"><link rel="prefetch" href="/assets/js/185.bcf4ab71.js"><link rel="prefetch" href="/assets/js/186.1cc02f6d.js"><link rel="prefetch" href="/assets/js/187.8b425fb7.js"><link rel="prefetch" href="/assets/js/188.44ccbd02.js"><link rel="prefetch" href="/assets/js/189.353b35e3.js"><link rel="prefetch" href="/assets/js/19.520992d5.js"><link rel="prefetch" href="/assets/js/190.c284595f.js"><link rel="prefetch" href="/assets/js/191.788ecc2d.js"><link rel="prefetch" href="/assets/js/193.da58aba3.js"><link rel="prefetch" href="/assets/js/194.6b1b1f4d.js"><link rel="prefetch" href="/assets/js/195.c31d5c39.js"><link rel="prefetch" href="/assets/js/196.f6670c4d.js"><link rel="prefetch" href="/assets/js/197.5a1f50ab.js"><link rel="prefetch" href="/assets/js/2.ab565158.js"><link rel="prefetch" href="/assets/js/20.69e29cdc.js"><link rel="prefetch" href="/assets/js/21.2fd424ad.js"><link rel="prefetch" href="/assets/js/22.d4c0be54.js"><link rel="prefetch" href="/assets/js/23.4bb90ecc.js"><link rel="prefetch" href="/assets/js/24.c01be6b2.js"><link rel="prefetch" href="/assets/js/25.c8833687.js"><link rel="prefetch" href="/assets/js/26.8042b555.js"><link rel="prefetch" href="/assets/js/27.0d5fa4c0.js"><link rel="prefetch" href="/assets/js/28.f9735b8b.js"><link rel="prefetch" href="/assets/js/29.3af53626.js"><link rel="prefetch" href="/assets/js/30.5f1b56d1.js"><link rel="prefetch" href="/assets/js/31.544b2649.js"><link rel="prefetch" href="/assets/js/32.aa321988.js"><link rel="prefetch" href="/assets/js/33.6aba2c86.js"><link rel="prefetch" href="/assets/js/34.e1bbff24.js"><link rel="prefetch" href="/assets/js/35.233f76e0.js"><link rel="prefetch" href="/assets/js/36.cb773972.js"><link rel="prefetch" href="/assets/js/37.393d9c59.js"><link rel="prefetch" href="/assets/js/38.e2d530c5.js"><link rel="prefetch" href="/assets/js/39.acaf1cc0.js"><link rel="prefetch" href="/assets/js/40.358f731e.js"><link rel="prefetch" href="/assets/js/41.ded24b7e.js"><link rel="prefetch" href="/assets/js/42.b9f683c3.js"><link rel="prefetch" href="/assets/js/43.c8fb3e66.js"><link rel="prefetch" href="/assets/js/44.633142da.js"><link rel="prefetch" href="/assets/js/45.6095e772.js"><link rel="prefetch" href="/assets/js/46.421d8c7a.js"><link rel="prefetch" href="/assets/js/47.da50fe47.js"><link rel="prefetch" href="/assets/js/48.15ff5726.js"><link rel="prefetch" href="/assets/js/49.b662e624.js"><link rel="prefetch" href="/assets/js/5.c1b8a209.js"><link rel="prefetch" href="/assets/js/50.a8bc75df.js"><link rel="prefetch" href="/assets/js/51.51e36ae7.js"><link rel="prefetch" href="/assets/js/52.54cc6e51.js"><link rel="prefetch" href="/assets/js/53.4173561d.js"><link rel="prefetch" href="/assets/js/54.7cab8416.js"><link rel="prefetch" href="/assets/js/55.3d7317d3.js"><link rel="prefetch" href="/assets/js/56.3c22255b.js"><link rel="prefetch" href="/assets/js/57.18e46e30.js"><link rel="prefetch" href="/assets/js/58.aad57f31.js"><link rel="prefetch" href="/assets/js/59.7897f6a7.js"><link rel="prefetch" href="/assets/js/6.3131f88a.js"><link rel="prefetch" href="/assets/js/60.5cd0051a.js"><link rel="prefetch" href="/assets/js/61.d9606403.js"><link rel="prefetch" href="/assets/js/62.aede9df0.js"><link rel="prefetch" href="/assets/js/63.2c30e554.js"><link rel="prefetch" href="/assets/js/64.18228ab7.js"><link rel="prefetch" href="/assets/js/65.27cb3fba.js"><link rel="prefetch" href="/assets/js/66.2fa6c2dc.js"><link rel="prefetch" href="/assets/js/67.d274a8df.js"><link rel="prefetch" href="/assets/js/68.3069cfcf.js"><link rel="prefetch" href="/assets/js/69.4c28600f.js"><link rel="prefetch" href="/assets/js/7.89e6165d.js"><link rel="prefetch" href="/assets/js/70.4175440c.js"><link rel="prefetch" href="/assets/js/71.2ee6b435.js"><link rel="prefetch" href="/assets/js/72.c75e3bb8.js"><link rel="prefetch" href="/assets/js/73.6f8b8211.js"><link rel="prefetch" href="/assets/js/74.6c7720cf.js"><link rel="prefetch" href="/assets/js/75.cccfb229.js"><link rel="prefetch" href="/assets/js/76.f022e5da.js"><link rel="prefetch" href="/assets/js/77.dab46206.js"><link rel="prefetch" href="/assets/js/78.ca574b2a.js"><link rel="prefetch" href="/assets/js/79.3d75e618.js"><link rel="prefetch" href="/assets/js/80.091749b1.js"><link rel="prefetch" href="/assets/js/81.14db0e21.js"><link rel="prefetch" href="/assets/js/82.8a2b1809.js"><link rel="prefetch" href="/assets/js/83.84a4b599.js"><link rel="prefetch" href="/assets/js/84.11d7c222.js"><link rel="prefetch" href="/assets/js/85.273d4388.js"><link rel="prefetch" href="/assets/js/86.fb40e20c.js"><link rel="prefetch" href="/assets/js/87.3316639e.js"><link rel="prefetch" href="/assets/js/88.dfc52200.js"><link rel="prefetch" href="/assets/js/89.8d615f6e.js"><link rel="prefetch" href="/assets/js/90.1d9f08bb.js"><link rel="prefetch" href="/assets/js/91.566813e7.js"><link rel="prefetch" href="/assets/js/92.d13c6f41.js"><link rel="prefetch" href="/assets/js/93.845c42a0.js"><link rel="prefetch" href="/assets/js/94.20a37b77.js"><link rel="prefetch" href="/assets/js/95.1a498005.js"><link rel="prefetch" href="/assets/js/96.39fa7f4b.js"><link rel="prefetch" href="/assets/js/97.50f7170e.js"><link rel="prefetch" href="/assets/js/98.dd2e15d6.js"><link rel="prefetch" href="/assets/js/99.ef7ea06a.js"><link rel="prefetch" href="/assets/js/vendors~docsearch.ae6b1de9.js">
    <link rel="stylesheet" href="/assets/css/0.styles.952d6952.css">
  </head>
  <body class="theme-mode-light">
    <div id="app" data-server-rendered="true"><div class="theme-container sidebar-open have-rightmenu"><header class="navbar blur"><div title="目录" class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/" class="home-link router-link-active"><img src="https://cdn.jsdelivr.net/gh/cmty256/imgs-blog@main/logo/白云.38zbldnhh180.jpg" alt="ShuangChenYue" class="logo"> <span class="site-name can-hide">ShuangChenYue</span></a> <div class="links"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/" class="nav-link">首页</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="CPP语言" class="dropdown-title"><!----> <span class="title" style="display:;">CPP语言</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/pages/c5bdd8/" class="nav-link">Cpp之旅</a></li><li class="dropdown-item"><!----> <a href="/pages/279e62/" class="nav-link">Cpp专栏</a></li><li class="dropdown-item"><!----> <a href="/pages/801755/" class="nav-link">Effective_CPP</a></li><li class="dropdown-item"><!----> <a href="/pages/6b2468/" class="nav-link">muduo网络库</a></li><li class="dropdown-item"><!----> <a href="/pages/5f8c9f/" class="nav-link">Unix环境高级编程</a></li><li class="dropdown-item"><!----> <a href="/pages/3f1d21/" class="nav-link">Cpp提高编程</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="计算机基础" class="dropdown-title"><!----> <span class="title" style="display:;">计算机基础</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/pages/7b1cb2/" class="nav-link">计算机网络</a></li><li class="dropdown-item"><!----> <a href="/pages/6048a8/" class="nav-link">操作系统</a></li><li class="dropdown-item"><!----> <a href="/pages/3b34ba/" class="nav-link">数据结构</a></li><li class="dropdown-item"><!----> <a href="/pages/412fe7/" class="nav-link">Linux</a></li><li class="dropdown-item"><!----> <a href="/pages/2dcfa1/" class="nav-link">算法</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="数据库" class="dropdown-title"><!----> <span class="title" style="display:;">数据库</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/pages/efa3f2/" class="nav-link">基础篇</a></li><li class="dropdown-item"><!----> <a href="/pages/ccc445/" class="nav-link">MySql</a></li><li class="dropdown-item"><!----> <a href="/pages/54616e/" class="nav-link">Redis</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="嵌入式软件开发" class="dropdown-title"><!----> <span class="title" style="display:;">嵌入式软件开发</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/pages/d142c2/" class="nav-link">电子嵌入式通信协议</a></li><li class="dropdown-item"><!----> <a href="/pages/4c6bf3/" class="nav-link">深入浅出SSD</a></li><li class="dropdown-item"><!----> <a href="/pages/d3f36a/" class="nav-link">文件系统</a></li><li class="dropdown-item"><!----> <a href="/pages/e0cca7/" class="nav-link">汇编语言</a></li><li class="dropdown-item"><!----> <a href="/pages/fab2d7/" class="nav-link">STM32</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="开发日常" class="dropdown-title"><!----> <span class="title" style="display:;">开发日常</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/pages/e472d1/" class="nav-link">随笔（持续更新）</a></li><li class="dropdown-item"><!----> <a href="/pages/71f6ae/" class="nav-link">Git知识总结</a></li><li class="dropdown-item"><!----> <a href="/pages/db6fb8/" class="nav-link">Git备忘清单</a></li><li class="dropdown-item"><!----> <a href="/pages/e1081f/" class="nav-link">Git 创建删除远程分支</a></li><li class="dropdown-item"><!----> <a href="/pages/777b8a/" class="nav-link">nvm使用小结</a></li><li class="dropdown-item"><!----> <a href="/pages/ee770e/" class="nav-link">虚拟机固定 IP 地址</a></li><li class="dropdown-item"><!----> <a href="/pages/1ab9a6/" class="nav-link">Shell 脚本学习笔记</a></li><li class="dropdown-item"><!----> <a href="/pages/411aa4/" class="nav-link">VScode 插件 CodeGeeX 使用教程</a></li><li class="dropdown-item"><!----> <a href="/pages/0d525d/" class="nav-link">KylinV10 将项目上传至 Github教程</a></li><li class="dropdown-item"><!----> <a href="/pages/907786/" class="nav-link">KylinV10 安装 MySQL 教程（可防踩雷）</a></li><li class="dropdown-item"><!----> <a href="/pages/a2d21e/" class="nav-link">kylinV10-SP1 安装 QT</a></li><li class="dropdown-item"><!----> <a href="/pages/b561cf/" class="nav-link">高并发内存池</a></li><li class="dropdown-item"><!----> <a href="/pages/6ab6d1/" class="nav-link">USBGUARD 项目编译环境配置</a></li><li class="dropdown-item"><!----> <a href="/pages/883f02/" class="nav-link">Power_Destory 项目</a></li><li class="dropdown-item"><!----> <a href="/pages/479472/" class="nav-link">U 盘清除工具编译教程</a></li><li class="dropdown-item"><!----> <a href="/pages/9c4241/" class="nav-link">个人博客代码推送教程</a></li><li class="dropdown-item"><!----> <a href="/pages/3ad765/" class="nav-link">SVN Trunk Branches的Merge操作</a></li><li class="dropdown-item"><!----> <a href="/pages/0c0ca8/" class="nav-link">如何高效阅读嵌入式项目代码</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="前端学习" class="dropdown-title"><!----> <span class="title" style="display:;">前端学习</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/pages/99897f/" class="nav-link">HTML与CSS</a></li><li class="dropdown-item"><!----> <a href="/pages/51542d/" class="nav-link">JS学习</a></li><li class="dropdown-item"><!----> <a href="/pages/803f9d/" class="nav-link">Vue3入门</a></li><li class="dropdown-item"><!----> <a href="/pages/ca4cfb/" class="nav-link">Vue3进阶</a></li><li class="dropdown-item"><!----> <a href="/pages/50e8d3/" class="nav-link">黑马Vue3</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="MFC" class="dropdown-title"><!----> <span class="title" style="display:;">MFC</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/pages/a4b108/" class="nav-link">MFC编程随记</a></li><li class="dropdown-item"><!----> <a href="/pages/41acbd/" class="nav-link">MFC实现ini配置文件的读取</a></li><li class="dropdown-item"><!----> <a href="/pages/951a7a/" class="nav-link">MFC实现点击列表头排序</a></li><li class="dropdown-item"><!----> <a href="/pages/a8598f/" class="nav-link">贴图法美化Button按钮</a></li><li class="dropdown-item"><!----> <a href="/pages/054516/" class="nav-link">MFC使用细节</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="闪存" class="dropdown-title"><!----> <span class="title" style="display:;">闪存</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/pages/b925b8/" class="nav-link">如何高效阅读嵌入式项目代码</a></li><li class="dropdown-item"><!----> <a href="/pages/28ec23/" class="nav-link">NAND Flash</a></li><li class="dropdown-item"><!----> <a href="/pages/62bf40/" class="nav-link">ARM 处理器</a></li><li class="dropdown-item"><!----> <a href="/pages/1a9374/" class="nav-link">嵌入式基础知识-存储器</a></li><li class="dropdown-item"><!----> <a href="/pages/aac5e3/" class="nav-link">闪存存储和制造技术概述</a></li><li class="dropdown-item"><!----> <a href="/pages/8f6056/" class="nav-link">芯片IO驱动力</a></li><li class="dropdown-item"><!----> <a href="/pages/d146b8/" class="nav-link">主流先进封装技术介绍</a></li><li class="dropdown-item"><!----> <a href="/pages/16f0ba/" class="nav-link">NAND Flash基础</a></li><li class="dropdown-item"><!----> <a href="/pages/90d8d0/" class="nav-link">基于PA算法的FTL引导</a></li><li class="dropdown-item"><!----> <a href="/pages/eb672b/" class="nav-link">SD逻辑擦除和物理擦除</a></li><li class="dropdown-item"><!----> <a href="/pages/747121/" class="nav-link">NAND Flash的SDR、ONFI、DDR接口</a></li><li class="dropdown-item"><!----> <a href="/pages/1eb351/" class="nav-link">【详解】Nand Flash必看知识</a></li><li class="dropdown-item"><!----> <a href="/pages/d2512a/" class="nav-link">【两万字详解】Nand Flash必看知识</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="面经" class="dropdown-title"><!----> <span class="title" style="display:;">面经</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/pages/d69946/" class="nav-link">虎牙C++技术面经</a></li><li class="dropdown-item"><!----> <a href="/pages/29251d/" aria-current="page" class="nav-link router-link-exact-active router-link-active">金山一面复习</a></li><li class="dropdown-item"><!----> <a href="/pages/c7c01f/" class="nav-link">完美世界秋招 C++ 游戏开发面经(Cpp部分)</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="其它" class="dropdown-title"><!----> <span class="title" style="display:;">其它</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/pages/fa256e/" class="nav-link">博客搭建</a></li><li class="dropdown-item"><!----> <a href="/pages/335531/" class="nav-link">网站收藏箱</a></li></ul></div></div> <!----></nav></div></header> <div class="sidebar-mask"></div> <div class="sidebar-hover-trigger"></div> <aside class="sidebar" style="display:none;"><!----> <nav class="nav-links"><div class="nav-item"><a href="/" class="nav-link">首页</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="CPP语言" class="dropdown-title"><!----> <span class="title" style="display:;">CPP语言</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/pages/c5bdd8/" class="nav-link">Cpp之旅</a></li><li class="dropdown-item"><!----> <a href="/pages/279e62/" class="nav-link">Cpp专栏</a></li><li class="dropdown-item"><!----> <a href="/pages/801755/" class="nav-link">Effective_CPP</a></li><li class="dropdown-item"><!----> <a href="/pages/6b2468/" class="nav-link">muduo网络库</a></li><li class="dropdown-item"><!----> <a href="/pages/5f8c9f/" class="nav-link">Unix环境高级编程</a></li><li class="dropdown-item"><!----> <a href="/pages/3f1d21/" class="nav-link">Cpp提高编程</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="计算机基础" class="dropdown-title"><!----> <span class="title" style="display:;">计算机基础</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/pages/7b1cb2/" class="nav-link">计算机网络</a></li><li class="dropdown-item"><!----> <a href="/pages/6048a8/" class="nav-link">操作系统</a></li><li class="dropdown-item"><!----> <a href="/pages/3b34ba/" class="nav-link">数据结构</a></li><li class="dropdown-item"><!----> <a href="/pages/412fe7/" class="nav-link">Linux</a></li><li class="dropdown-item"><!----> <a href="/pages/2dcfa1/" class="nav-link">算法</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="数据库" class="dropdown-title"><!----> <span class="title" style="display:;">数据库</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/pages/efa3f2/" class="nav-link">基础篇</a></li><li class="dropdown-item"><!----> <a href="/pages/ccc445/" class="nav-link">MySql</a></li><li class="dropdown-item"><!----> <a href="/pages/54616e/" class="nav-link">Redis</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="嵌入式软件开发" class="dropdown-title"><!----> <span class="title" style="display:;">嵌入式软件开发</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/pages/d142c2/" class="nav-link">电子嵌入式通信协议</a></li><li class="dropdown-item"><!----> <a href="/pages/4c6bf3/" class="nav-link">深入浅出SSD</a></li><li class="dropdown-item"><!----> <a href="/pages/d3f36a/" class="nav-link">文件系统</a></li><li class="dropdown-item"><!----> <a href="/pages/e0cca7/" class="nav-link">汇编语言</a></li><li class="dropdown-item"><!----> <a href="/pages/fab2d7/" class="nav-link">STM32</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="开发日常" class="dropdown-title"><!----> <span class="title" style="display:;">开发日常</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/pages/e472d1/" class="nav-link">随笔（持续更新）</a></li><li class="dropdown-item"><!----> <a href="/pages/71f6ae/" class="nav-link">Git知识总结</a></li><li class="dropdown-item"><!----> <a href="/pages/db6fb8/" class="nav-link">Git备忘清单</a></li><li class="dropdown-item"><!----> <a href="/pages/e1081f/" class="nav-link">Git 创建删除远程分支</a></li><li class="dropdown-item"><!----> <a href="/pages/777b8a/" class="nav-link">nvm使用小结</a></li><li class="dropdown-item"><!----> <a href="/pages/ee770e/" class="nav-link">虚拟机固定 IP 地址</a></li><li class="dropdown-item"><!----> <a href="/pages/1ab9a6/" class="nav-link">Shell 脚本学习笔记</a></li><li class="dropdown-item"><!----> <a href="/pages/411aa4/" class="nav-link">VScode 插件 CodeGeeX 使用教程</a></li><li class="dropdown-item"><!----> <a href="/pages/0d525d/" class="nav-link">KylinV10 将项目上传至 Github教程</a></li><li class="dropdown-item"><!----> <a href="/pages/907786/" class="nav-link">KylinV10 安装 MySQL 教程（可防踩雷）</a></li><li class="dropdown-item"><!----> <a href="/pages/a2d21e/" class="nav-link">kylinV10-SP1 安装 QT</a></li><li class="dropdown-item"><!----> <a href="/pages/b561cf/" class="nav-link">高并发内存池</a></li><li class="dropdown-item"><!----> <a href="/pages/6ab6d1/" class="nav-link">USBGUARD 项目编译环境配置</a></li><li class="dropdown-item"><!----> <a href="/pages/883f02/" class="nav-link">Power_Destory 项目</a></li><li class="dropdown-item"><!----> <a href="/pages/479472/" class="nav-link">U 盘清除工具编译教程</a></li><li class="dropdown-item"><!----> <a href="/pages/9c4241/" class="nav-link">个人博客代码推送教程</a></li><li class="dropdown-item"><!----> <a href="/pages/3ad765/" class="nav-link">SVN Trunk Branches的Merge操作</a></li><li class="dropdown-item"><!----> <a href="/pages/0c0ca8/" class="nav-link">如何高效阅读嵌入式项目代码</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="前端学习" class="dropdown-title"><!----> <span class="title" style="display:;">前端学习</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/pages/99897f/" class="nav-link">HTML与CSS</a></li><li class="dropdown-item"><!----> <a href="/pages/51542d/" class="nav-link">JS学习</a></li><li class="dropdown-item"><!----> <a href="/pages/803f9d/" class="nav-link">Vue3入门</a></li><li class="dropdown-item"><!----> <a href="/pages/ca4cfb/" class="nav-link">Vue3进阶</a></li><li class="dropdown-item"><!----> <a href="/pages/50e8d3/" class="nav-link">黑马Vue3</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="MFC" class="dropdown-title"><!----> <span class="title" style="display:;">MFC</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/pages/a4b108/" class="nav-link">MFC编程随记</a></li><li class="dropdown-item"><!----> <a href="/pages/41acbd/" class="nav-link">MFC实现ini配置文件的读取</a></li><li class="dropdown-item"><!----> <a href="/pages/951a7a/" class="nav-link">MFC实现点击列表头排序</a></li><li class="dropdown-item"><!----> <a href="/pages/a8598f/" class="nav-link">贴图法美化Button按钮</a></li><li class="dropdown-item"><!----> <a href="/pages/054516/" class="nav-link">MFC使用细节</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="闪存" class="dropdown-title"><!----> <span class="title" style="display:;">闪存</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/pages/b925b8/" class="nav-link">如何高效阅读嵌入式项目代码</a></li><li class="dropdown-item"><!----> <a href="/pages/28ec23/" class="nav-link">NAND Flash</a></li><li class="dropdown-item"><!----> <a href="/pages/62bf40/" class="nav-link">ARM 处理器</a></li><li class="dropdown-item"><!----> <a href="/pages/1a9374/" class="nav-link">嵌入式基础知识-存储器</a></li><li class="dropdown-item"><!----> <a href="/pages/aac5e3/" class="nav-link">闪存存储和制造技术概述</a></li><li class="dropdown-item"><!----> <a href="/pages/8f6056/" class="nav-link">芯片IO驱动力</a></li><li class="dropdown-item"><!----> <a href="/pages/d146b8/" class="nav-link">主流先进封装技术介绍</a></li><li class="dropdown-item"><!----> <a href="/pages/16f0ba/" class="nav-link">NAND Flash基础</a></li><li class="dropdown-item"><!----> <a href="/pages/90d8d0/" class="nav-link">基于PA算法的FTL引导</a></li><li class="dropdown-item"><!----> <a href="/pages/eb672b/" class="nav-link">SD逻辑擦除和物理擦除</a></li><li class="dropdown-item"><!----> <a href="/pages/747121/" class="nav-link">NAND Flash的SDR、ONFI、DDR接口</a></li><li class="dropdown-item"><!----> <a href="/pages/1eb351/" class="nav-link">【详解】Nand Flash必看知识</a></li><li class="dropdown-item"><!----> <a href="/pages/d2512a/" class="nav-link">【两万字详解】Nand Flash必看知识</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="面经" class="dropdown-title"><!----> <span class="title" style="display:;">面经</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/pages/d69946/" class="nav-link">虎牙C++技术面经</a></li><li class="dropdown-item"><!----> <a href="/pages/29251d/" aria-current="page" class="nav-link router-link-exact-active router-link-active">金山一面复习</a></li><li class="dropdown-item"><!----> <a href="/pages/c7c01f/" class="nav-link">完美世界秋招 C++ 游戏开发面经(Cpp部分)</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="其它" class="dropdown-title"><!----> <span class="title" style="display:;">其它</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/pages/fa256e/" class="nav-link">博客搭建</a></li><li class="dropdown-item"><!----> <a href="/pages/335531/" class="nav-link">网站收藏箱</a></li></ul></div></div> <!----></nav>  <ul class="sidebar-links"><li><a href="/pages/d69946/" class="sidebar-link">虎牙C++技术面经</a></li><li><a href="/pages/29251d/" aria-current="page" class="active sidebar-link">金山一面复习</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level2"><a href="/pages/29251d/#_1、静态变量生命周期和普通变量比较" class="sidebar-link">1、静态变量生命周期和普通变量比较？</a></li><li class="sidebar-sub-header level2"><a href="/pages/29251d/#_2、什么时候创建虚函数表" class="sidebar-link">2、什么时候创建虚函数表？</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level3"><a href="/pages/29251d/#虚函数的定义" class="sidebar-link">虚函数的定义：</a></li><li class="sidebar-sub-header level3"><a href="/pages/29251d/#派生类中的重写" class="sidebar-link">派生类中的重写：</a></li><li class="sidebar-sub-header level3"><a href="/pages/29251d/#虚函数表的创建" class="sidebar-link">虚函数表的创建：</a></li><li class="sidebar-sub-header level3"><a href="/pages/29251d/#虚函数表的指针" class="sidebar-link">虚函数表的指针：</a></li><li class="sidebar-sub-header level3"><a href="/pages/29251d/#运行时动态绑定" class="sidebar-link">运行时动态绑定：</a></li></ul></li><li class="sidebar-sub-header level2"><a href="/pages/29251d/#_3、虚函数指针会不会变-什么时候初始化-在析构里会不会变-析构函数能访问虚函数吗" class="sidebar-link">3、虚函数指针会不会变，什么时候初始化，在析构里会不会变，析构函数能访问虚函数吗？</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level3"><a href="/pages/29251d/#初始化时创建" class="sidebar-link">初始化时创建：</a></li><li class="sidebar-sub-header level3"><a href="/pages/29251d/#不会在析构函数中改变" class="sidebar-link">不会在析构函数中改变：</a></li><li class="sidebar-sub-header level3"><a href="/pages/29251d/#虚函数的调用" class="sidebar-link">虚函数的调用：</a></li><li class="sidebar-sub-header level3"><a href="/pages/29251d/#注意虚函数表指针变化的情况" class="sidebar-link">注意虚函数表指针变化的情况：</a></li></ul></li><li class="sidebar-sub-header level2"><a href="/pages/29251d/#_4、静态函数可以访问非静态成员变量吗为什么" class="sidebar-link">4、静态函数可以访问非静态成员变量吗为什么？</a></li><li class="sidebar-sub-header level2"><a href="/pages/29251d/#_5、编译实现重载" class="sidebar-link">5、编译实现重载？</a></li><li class="sidebar-sub-header level2"><a href="/pages/29251d/#_6、静态变量新特性保证原子性" class="sidebar-link">6、静态变量新特性保证原子性？</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level3"><a href="/pages/29251d/#_1、初始化保护" class="sidebar-link">1、初始化保护：</a></li><li class="sidebar-sub-header level3"><a href="/pages/29251d/#_2、内存模型" class="sidebar-link">2、内存模型：</a></li></ul></li><li class="sidebar-sub-header level2"><a href="/pages/29251d/#_7、静态局部变量的使用" class="sidebar-link">7、静态局部变量的使用？</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level3"><a href="/pages/29251d/#初始化保护" class="sidebar-link">初始化保护：</a></li><li class="sidebar-sub-header level3"><a href="/pages/29251d/#作用域" class="sidebar-link">作用域：</a></li><li class="sidebar-sub-header level3"><a href="/pages/29251d/#内存分配" class="sidebar-link">内存分配：</a></li><li class="sidebar-sub-header level3"><a href="/pages/29251d/#线程安全性" class="sidebar-link">线程安全性：</a></li><li class="sidebar-sub-header level3"><a href="/pages/29251d/#初始值" class="sidebar-link">初始值：</a></li><li class="sidebar-sub-header level3"><a href="/pages/29251d/#保持状态" class="sidebar-link">保持状态：</a></li></ul></li><li class="sidebar-sub-header level2"><a href="/pages/29251d/#_8、四种cast用在什么时候-dynamic什么时候用-不这么用返回值是什么" class="sidebar-link">8、四种cast用在什么时候，dynamic什么时候用，不这么用返回值是什么？</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level3"><a href="/pages/29251d/#static-cast" class="sidebar-link">static_cast</a></li><li class="sidebar-sub-header level3"><a href="/pages/29251d/#dynamic-cast" class="sidebar-link">dynamic_cast</a></li><li class="sidebar-sub-header level3"><a href="/pages/29251d/#const-cast" class="sidebar-link">const_cast</a></li><li class="sidebar-sub-header level3"><a href="/pages/29251d/#reinterpret-cast" class="sidebar-link">reinterpret_cast</a></li></ul></li><li class="sidebar-sub-header level2"><a href="/pages/29251d/#_9、vector数组迭代器失效时机-如何使用迭代器删除vector-erase返回值" class="sidebar-link">9、Vector数组迭代器失效时机，如何使用迭代器删除vector，erase返回值？</a></li><li class="sidebar-sub-header level2"><a href="/pages/29251d/#_10、map数据结构-struct插入map需要注意什么" class="sidebar-link">10、Map数据结构，struct插入map需要注意什么</a></li><li class="sidebar-sub-header level2"><a href="/pages/29251d/#_11、进程间通信" class="sidebar-link">11、进程间通信？</a></li><li class="sidebar-sub-header level2"><a href="/pages/29251d/#_12、如何实现多次运行一个程序只有一个后台进程" class="sidebar-link">12、如何实现多次运行一个程序只有一个后台进程？</a></li><li class="sidebar-sub-header level2"><a href="/pages/29251d/#_13、tcp三次握手为什么不是2次或者4次" class="sidebar-link">13、Tcp三次握手为什么不是2次或者4次？</a></li><li class="sidebar-sub-header level2"><a href="/pages/29251d/#_14、二叉树最大深度" class="sidebar-link">14、二叉树最大深度？</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level3"><a href="/pages/29251d/#思路-双栈法" class="sidebar-link">思路：双栈法</a></li><li class="sidebar-sub-header level3"><a href="/pages/29251d/#思路-递归" class="sidebar-link">思路：递归</a></li></ul></li><li class="sidebar-sub-header level2"><a href="/pages/29251d/#_15、反转链表" class="sidebar-link">15、反转链表？</a></li></ul></li><li><a href="/pages/c7c01f/" class="sidebar-link">完美世界秋招 C++ 游戏开发面经（Cpp部分）</a></li></ul> </aside> <div><main class="page"><div class="theme-vdoing-wrapper "><div class="articleInfo-wrap" data-v-06225672><div class="articleInfo" data-v-06225672><ul class="breadcrumbs" data-v-06225672><li data-v-06225672><a href="/" title="首页" class="iconfont icon-home router-link-active" data-v-06225672></a></li> <li data-v-06225672><span data-v-06225672>面经</span></li></ul> <div class="info" data-v-06225672><div title="作者" class="author iconfont icon-touxiang" data-v-06225672><a href="javascript:;" data-v-06225672>霜晨月</a></div> <div title="创建时间" class="date iconfont icon-riqi" data-v-06225672><a href="javascript:;" data-v-06225672>2023-12-05</a></div> <!----></div></div></div> <!----> <div class="content-wrapper"><div class="right-menu-wrapper"><div class="right-menu-margin"><div class="right-menu-title">目录</div> <div class="right-menu-content"></div></div></div> <h1><img src="">金山一面复习<!----></h1> <!----> <div class="theme-vdoing-content content__default"><h1 id="金山一面复习"><a href="#金山一面复习" class="header-anchor">#</a> 金山一面复习</h1> <blockquote><p>时间：2023.11.7</p></blockquote> <h2 id="_1、静态变量生命周期和普通变量比较"><a href="#_1、静态变量生命周期和普通变量比较" class="header-anchor">#</a> 1、静态变量生命周期和普通变量比较？</h2> <ol><li>生命周期：
<ol><li>普通变量（自动变量）：自动变量的生命周期与其所在的代码块（作用域）相关。它们在离开其定义的作用域时被销毁，通常是栈上分配的。</li> <li>静态变量（静态存储期变量）：静态变量的生命周期在整个程序运行期间，它们在程序启动时分配内存，在程序结束时才会被释放。静态变量可以存储在全局存储区或静态存储区。</li></ol></li> <li>作用域：
<ol><li>普通变量：普通变量的作用域通常仅限于定义它们的代码块，它们不能被其他代码块访问。</li> <li>静态变量：静态变量可以具有更广泛的作用域，例如，如果它们在全局范围内声明，它们可以被整个程序访问。</li></ol></li> <li>初始化：
<ol><li>普通变量：普通变量在声明时不会自动初始化，除非显式赋初值。</li> <li>静态变量：静态变量在定义时如果不显式初始化，会自动初始化为零（对于基本数据类型）或空（对于类对象）。</li></ol></li> <li>存储位置：
<ol><li>普通变量：普通变量通常分配在栈上，每次函数调用时都会创建新的实例。</li> <li>静态变量：静态变量可以存储在全局存储区或静态存储区。全局变量在程序启动时初始化，它们的值在程序的整个生命周期内都可用。</li></ol></li></ol> <h2 id="_2、什么时候创建虚函数表"><a href="#_2、什么时候创建虚函数表" class="header-anchor">#</a> 2、什么时候创建虚函数表？</h2> <h3 id="虚函数的定义"><a href="#虚函数的定义" class="header-anchor">#</a> 虚函数的定义：</h3> <p>虚函数是通过在基类中声明的。虚函数的定义包括返回类型、函数名和参数列表。</p> <div class="language-CPP line-numbers-mode"><pre class="language-cpp"><code><span class="token keyword">class</span> <span class="token class-name">Base</span> <span class="token punctuation">{</span>
    <span class="token keyword">public</span><span class="token operator">:</span>
    <span class="token keyword">virtual</span> <span class="token keyword">void</span> <span class="token function">foo</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment">/* Base class implementation */</span><span class="token punctuation">}</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><h3 id="派生类中的重写"><a href="#派生类中的重写" class="header-anchor">#</a> 派生类中的重写：</h3> <p>派生类可以重写虚函数，这意味着它可以提供自己的实现版本。在派生类中重新定义虚函数时，需要使用 <code>virtual</code> 关键字来保持虚函数性质。</p> <div class="language-CPP line-numbers-mode"><pre class="language-cpp"><code><span class="token keyword">class</span> <span class="token class-name">Derived</span> <span class="token operator">:</span> <span class="token base-clause"><span class="token keyword">public</span> <span class="token class-name">Base</span></span> <span class="token punctuation">{</span>
    <span class="token keyword">public</span><span class="token operator">:</span>
    <span class="token keyword">virtual</span> <span class="token keyword">void</span> <span class="token function">foo</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">override</span> <span class="token punctuation">{</span><span class="token comment">/* Derived class implementation */</span><span class="token punctuation">}</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><h3 id="虚函数表的创建"><a href="#虚函数表的创建" class="header-anchor">#</a> 虚函数表的创建：</h3> <p>虚函数表在编译时由C++编译器自动生成。对于每个具有虚函数的类，编译器会生成一个虚函数表，其中包含了该类的虚函数指针。这个虚函数表通常位于类的内部，它是静态的，一旦创建就不会更改。</p> <h3 id="虚函数表的指针"><a href="#虚函数表的指针" class="header-anchor">#</a> 虚函数表的指针：</h3> <p>对于每个包含虚函数的类，编译器会在类的内部添加一个指向虚函数表的指针，通常位于对象的内存布局的最前面。这个指针被称为虚函数表指针（vptr）。</p> <h3 id="运行时动态绑定"><a href="#运行时动态绑定" class="header-anchor">#</a> 运行时动态绑定：</h3> <p>当你通过基类指针或引用调用虚函数时，实际执行的是派生类中的版本（如果派生类重新定义了虚函数）。这是因为虚函数表指针（vptr）指向了正确的虚函数表，允许在运行时根据对象的类型进行动态绑定。</p> <div class="language-CPP line-numbers-mode"><pre class="language-cpp"><code>Base <span class="token operator">*</span>ptr <span class="token operator">=</span> <span class="token keyword">new</span> Derived<span class="token punctuation">;</span>
ptr<span class="token operator">-&gt;</span><span class="token function">foo</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//调用Derived类中的foo()</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><h2 id="_3、虚函数指针会不会变-什么时候初始化-在析构里会不会变-析构函数能访问虚函数吗"><a href="#_3、虚函数指针会不会变-什么时候初始化-在析构里会不会变-析构函数能访问虚函数吗" class="header-anchor">#</a> 3、虚函数指针会不会变，什么时候初始化，在析构里会不会变，析构函数能访问虚函数吗？</h2> <h3 id="初始化时创建"><a href="#初始化时创建" class="header-anchor">#</a> 初始化时创建：</h3> <p>虚函数表指针在对象创建时就被初始化。当你创建一个类的实例（对象）时，其中包括一个指向该类虚函数表的虚函数表指针。这个指针是在对象构造过程中初始化的。</p> <h3 id="不会在析构函数中改变"><a href="#不会在析构函数中改变" class="header-anchor">#</a> 不会在析构函数中改变：</h3> <p>虚函数表指针通常在对象的整个生命周期内保持不变。这意味着即使在对象的析构函数中，虚函数表指针也不会改变。析构函数是用于对象销毁的，不负责改变虚函数表指针。</p> <h3 id="虚函数的调用"><a href="#虚函数的调用" class="header-anchor">#</a> 虚函数的调用：</h3> <p>虚函数表指针的主要作用是支持运行时多态性。通过这个指针，程序可以在运行时查找并调用正确的虚函数版本。在析构函数中，你可以调用虚函数，但需要注意的是析构函数本身不会改变虚函数表指针。在析构函数中调用虚函数时，通常执行的是对象的类型，而不是基类的类型。</p> <h3 id="注意虚函数表指针变化的情况"><a href="#注意虚函数表指针变化的情况" class="header-anchor">#</a> 注意虚函数表指针变化的情况：</h3> <p>在一些特殊情况下，虚函数表指针可能会变化。例如，当一个对象通过复制构造函数复制时，复制的对象将有自己的虚函数表指针。这通常发生在基类和派生类之间的复制。另外，如果你使用虚继承，虚函数表指针也可能会更改。</p> <h2 id="_4、静态函数可以访问非静态成员变量吗为什么"><a href="#_4、静态函数可以访问非静态成员变量吗为什么" class="header-anchor">#</a> 4、静态函数可以访问非静态成员变量吗为什么？</h2> <p><strong>静态成员函数（静态方法）可以访问非静态成员变量。</strong></p> <ol><li>静态函数（静态成员函数）：静态函数是与类关联，而不是与类的实例（对象）关联的函数。这意味着它不依赖于特定的对象，可以通过类名直接调用。静态函数通常用于执行与类本身相关的操作，而不涉及特定对象的状态。</li> <li>非静态成员变量：非静态成员变量是与类的实例（对象）关联的变量。每个类的对象都有其自己的一组非静态成员变量，它们可以存储对象特定的状态信息。</li></ol> <p><strong>现在来看静态函数访问非静态成员变量的情况：</strong></p> <ul><li>可以访问静态成员变量：静态函数可以自由地访问同一类的静态成员变量，因为这些成员变量与类相关，而不是与对象相关。在静态函数中，你可以使用类名或<code>this</code>指针（在类的范围内）来访问静态成员变量。</li> <li>不能直接访问非静态成员变量：静态函数不能直接访问特定对象的非静态成员变量。这是因为静态函数没有&quot;this&quot;指针，它不知道它应该关联到哪个对象的非静态成员变量。</li> <li>需要对象实例：如果静态函数需要访问特定对象的非静态成员变量，它必须通过传递对象实例作为参数或在函数内部创建对象实例，然后使用该对象实例来访问非静态成员变量。</li></ul> <p>示例：</p> <div class="language-CPP line-numbers-mode"><pre class="language-cpp"><code><span class="token keyword">class</span> <span class="token class-name">MyClass</span> <span class="token punctuation">{</span>
    <span class="token keyword">public</span><span class="token operator">:</span>
    <span class="token keyword">int</span> nonStaticVar<span class="token punctuation">;</span><span class="token comment">//非静态成员变量</span>
    <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">StaticFunction</span><span class="token punctuation">(</span>MyClass<span class="token operator">&amp;</span> obj<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        obj<span class="token punctuation">.</span>nonStaticVar <span class="token operator">=</span> <span class="token number">42</span><span class="token punctuation">;</span><span class="token comment">//通过对象实例访问非静态成员变量</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><h2 id="_5、编译实现重载"><a href="#_5、编译实现重载" class="header-anchor">#</a> 5、编译实现重载？</h2> <p>**函数重载（Function Overloading）是一种允许你定义多个同名函数，但它们具有不同的参数列表的机制。**编译器会根据不同的参数列表来决定调用哪个重载函数。函数重载使你能够使用相同的函数名来处理不同类型的数据或不同数量的参数，从而提高了代码的可读性和复用性。</p> <ol><li>函数名称相同：在函数重载中，你可以定义多个具有相同名称的函数，但它们的参数列表不同。</li> <li>参数列表不同：参数列表包括参数的数量、参数的类型、参数的顺序等。至少需要有一个方面在参数列表中不同，否则编译器将无法区分这些函数。</li> <li>返回类型不同：重载函数的返回类型可以不同，但它通常不是编译器用于函数重载决策的关键因素。编译器主要关注参数列表。</li> <li>与函数调用相关：编译器会根据函数调用时提供的参数来决定调用哪个重载函数。</li></ol> <div class="language-CPP line-numbers-mode"><pre class="language-cpp"><code><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;iostream&gt;</span></span>
<span class="token keyword">void</span> <span class="token function">print</span><span class="token punctuation">(</span><span class="token keyword">int</span> value<span class="token punctuation">)</span> <span class="token punctuation">{</span>
	std<span class="token double-colon punctuation">::</span>cout <span class="token operator">&lt;&lt;</span> <span class="token string">&quot;Printing integer: &quot;</span> <span class="token operator">&lt;&lt;</span> value <span class="token operator">&lt;&lt;</span> std<span class="token double-colon punctuation">::</span>endl<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token keyword">void</span> <span class="token function">print</span><span class="token punctuation">(</span><span class="token keyword">double</span> value<span class="token punctuation">)</span> <span class="token punctuation">{</span>
	std<span class="token double-colon punctuation">::</span>cout <span class="token operator">&lt;&lt;</span> <span class="token string">&quot;Printing double: &quot;</span> <span class="token operator">&lt;&lt;</span> value <span class="token operator">&lt;&lt;</span> std<span class="token double-colon punctuation">::</span>endl<span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token keyword">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">int</span> intVar <span class="token operator">=</span> <span class="token number">42</span><span class="token punctuation">;</span>
    <span class="token keyword">double</span> doubleVar <span class="token operator">=</span> <span class="token number">3.14159</span><span class="token punctuation">;</span>
    <span class="token function">print</span><span class="token punctuation">(</span>intVar<span class="token punctuation">)</span><span class="token punctuation">;</span>	 <span class="token comment">//调用第一个重载函数</span>
    <span class="token function">print</span><span class="token punctuation">(</span>doubleVar<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//调用第二个重载函数</span>
    <span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br></div></div><h2 id="_6、静态变量新特性保证原子性"><a href="#_6、静态变量新特性保证原子性" class="header-anchor">#</a> 6、静态变量新特性保证原子性？</h2> <h3 id="_1、初始化保护"><a href="#_1、初始化保护" class="header-anchor">#</a> 1、初始化保护：</h3> <p>C++11规定了静态局部变量的初始化必须是线程安全的。这意味着在多线程环境中，多个线程第一次进入函数并尝试初始化同一个静态局部变量时，只有一个线程会执行初始化，其他线程会等待。这种机制确保了静态局部变量的初始化是线程安全的。</p> <div class="language-CPP line-numbers-mode"><pre class="language-cpp"><code><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;iostream&gt;</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;thread&gt;</span></span>
<span class="token keyword">void</span> <span class="token function">foo</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
    <span class="token keyword">static</span> <span class="token keyword">int</span> x <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span><span class="token comment">//C++11以后的标准保证x的初始化是线程安全的</span>
    std<span class="token double-colon punctuation">::</span>cout <span class="token operator">&lt;&lt;</span> x<span class="token operator">++</span> <span class="token operator">&lt;&lt;</span> std<span class="token double-colon punctuation">::</span>endl<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token keyword">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
    std<span class="token double-colon punctuation">::</span>thread <span class="token function">t1</span><span class="token punctuation">(</span>foo<span class="token punctuation">)</span><span class="token punctuation">;</span>
    std<span class="token double-colon punctuation">::</span>thread <span class="token function">t2</span><span class="token punctuation">(</span>foo<span class="token punctuation">)</span><span class="token punctuation">;</span>
    t1<span class="token punctuation">.</span><span class="token function">join</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    t2<span class="token punctuation">.</span><span class="token function">join</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br></div></div><h3 id="_2、内存模型"><a href="#_2、内存模型" class="header-anchor">#</a> 2、内存模型：</h3> <p>C++11引入了内存模型，定义了多线程程序中的内存访问行为。这使得对静态局部变量的访问在多线程环境下更加可控。</p> <p>C++11引入的内存模型定义了一些术语，如&quot;原子操作&quot;、&quot;memory order&quot;等，允许程序员更精确地控制多线程环境中的内存访问。可以使用<code>std::atomic</code>类型来声明原子变量，确保它们的操作是线程安全的。</p> <div class="language-CPP line-numbers-mode"><pre class="language-cpp"><code><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;iostream&gt;</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;thread&gt;</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;atomic&gt;</span></span>
std<span class="token double-colon punctuation">::</span>atomic<span class="token operator">&lt;</span><span class="token keyword">int</span><span class="token operator">&gt;</span> <span class="token function">x</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//使用std::atomic声明原子变量</span>
<span class="token keyword">void</span> <span class="token function">foo</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    x<span class="token punctuation">.</span><span class="token function">fetch_add</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span>std<span class="token double-colon punctuation">::</span>memory_order_relaxed<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//使用原子操作</span>
<span class="token punctuation">}</span>
<span class="token keyword">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	std<span class="token double-colon punctuation">::</span>thread <span class="token function">t1</span><span class="token punctuation">(</span>foo<span class="token punctuation">)</span><span class="token punctuation">;</span>
    std<span class="token double-colon punctuation">::</span>thread <span class="token function">t2</span><span class="token punctuation">(</span>foo<span class="token punctuation">)</span><span class="token punctuation">;</span>
    t1<span class="token punctuation">.</span><span class="token function">join</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    t2<span class="token punctuation">.</span><span class="token function">join</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    std<span class="token double-colon punctuation">::</span>cout <span class="token operator">&lt;&lt;</span> x<span class="token punctuation">.</span><span class="token function">load</span><span class="token punctuation">(</span>std<span class="token double-colon punctuation">::</span>memory_order_relaxed<span class="token punctuation">)</span> <span class="token operator">&lt;&lt;</span> std<span class="token double-colon punctuation">::</span>endl<span class="token punctuation">;</span>
    <span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br></div></div><ul><li>在这个例子中，<code>x</code>是一个原子整数，可以被多个线程同时访问而不引发竞态条件或数据竞争。</li> <li><code>foo</code>函数通过<code>x.fetch_add(1, std::memory_order_relaxed)</code>来增加<code>x</code>的值，这个操作是原子的，不会被中断，因此多个线程可以同时执行它。</li> <li>主线程使用<code>x.load(std::memory_order_relaxed)</code>来加载<code>x</code>的值，并输出它。这个例子中使用的是<code>std::memory_order_relaxed</code>，它是一种内存顺序（memory order）选项，表示对<code>x</code>的操作可以是松散的，没有特定的内存顺序要求。这意味着编译器和硬件可以以不同的顺序执行操作，只要最终结果是正确的。</li></ul> <h2 id="_7、静态局部变量的使用"><a href="#_7、静态局部变量的使用" class="header-anchor">#</a> 7、静态局部变量的使用？</h2> <p>静态局部变量是在函数内部定义的局部变量，但它与普通局部变量不同，它的生命周期在整个程序运行期间保持不变。这意味着它只会被初始化一次，并且在后续的函数调用中保持其值，直到程序终止。静态局部变量通常用关键字 <code>static</code> 来声明。</p> <h3 id="初始化保护"><a href="#初始化保护" class="header-anchor">#</a> 初始化保护：</h3> <p>静态局部变量的初始化仅在第一次进入包含它的函数时执行，后续的函数调用将跳过初始化过程。这使得静态局部变量适合用于一些需要在多次函数调用中保持状态的情况。</p> <div class="language-CPP line-numbers-mode"><pre class="language-cpp"><code><span class="token keyword">int</span> <span class="token function">myFunction</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">static</span> <span class="token keyword">int</span> count <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span><span class="token comment">//静态局部变量，只在第一次调用时初始化</span>
    count<span class="token operator">++</span><span class="token punctuation">;</span>
    <span class="token keyword">return</span> count<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><h3 id="作用域"><a href="#作用域" class="header-anchor">#</a> 作用域：</h3> <p>静态局部变量的作用域限于包含它的函数。这意味着它只能在该函数内部访问，不能被其他函数直接访问。</p> <h3 id="内存分配"><a href="#内存分配" class="header-anchor">#</a> 内存分配：</h3> <p>静态局部变量通常存储在程序的全局数据区中，而不是堆栈中。因此，它们在程序启动时分配内存，并在程序终止时释放内存。</p> <h3 id="线程安全性"><a href="#线程安全性" class="header-anchor">#</a> 线程安全性：</h3> <p>静态局部变量的初始化是线程安全的。C++11以后的标准规定了这一点，确保只有一个线程在初始化时访问该变量。</p> <h3 id="初始值"><a href="#初始值" class="header-anchor">#</a> 初始值：</h3> <p>如果不明确初始化静态局部变量，它们将被自动初始化为零值（对于内置类型，如整数）或者空值（对于类类型，如指针或字符串）。</p> <h3 id="保持状态"><a href="#保持状态" class="header-anchor">#</a> 保持状态：</h3> <p>静态局部变量通常用于需要在多次函数调用之间保持状态的情况，如计数器、状态标志或缓存。</p> <div class="language-CPP line-numbers-mode"><pre class="language-cpp"><code><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;iostream&gt;</span></span>
<span class="token keyword">int</span> <span class="token function">myFunction</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
    <span class="token keyword">static</span> <span class="token keyword">int</span> count <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span><span class="token comment">//静态局部变量，旨在i第一次调用时初始化</span>
    count<span class="token operator">++</span><span class="token punctuation">;</span>
    <span class="token keyword">return</span> count<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token keyword">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
    std<span class="token double-colon punctuation">::</span>count <span class="token operator">&lt;&lt;</span> <span class="token function">myFunction</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">&lt;&lt;</span> std<span class="token double-colon punctuation">::</span>endl<span class="token punctuation">;</span><span class="token comment">//输出1</span>
    std<span class="token double-colon punctuation">::</span>count <span class="token operator">&lt;&lt;</span> <span class="token function">myFunction</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">&lt;&lt;</span> std<span class="token double-colon punctuation">::</span>endl<span class="token punctuation">;</span><span class="token comment">//输出2</span>
    std<span class="token double-colon punctuation">::</span>count <span class="token operator">&lt;&lt;</span> <span class="token function">myFunction</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">&lt;&lt;</span> std<span class="token double-colon punctuation">::</span>endl<span class="token punctuation">;</span><span class="token comment">//输出3</span>
    <span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br></div></div><h2 id="_8、四种cast用在什么时候-dynamic什么时候用-不这么用返回值是什么"><a href="#_8、四种cast用在什么时候-dynamic什么时候用-不这么用返回值是什么" class="header-anchor">#</a> 8、四种cast用在什么时候，dynamic什么时候用，不这么用返回值是什么？</h2> <h3 id="static-cast"><a href="#static-cast" class="header-anchor">#</a> static_cast</h3> <ul><li>用途：主要用于基本类型之间的转换，如将整数转换为浮点数，或者将指针或引用从一个类型转换为另一个相关类型。</li> <li>安全性：在编译时执行，较为安全。不进行运行时类型检查。</li></ul> <p>示例：</p> <div class="language-CPP line-numbers-mode"><pre class="language-cpp"><code><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">42</span><span class="token punctuation">;</span>
<span class="token keyword">double</span> d <span class="token operator">=</span> <span class="token generic-function"><span class="token function">static_cast</span><span class="token generic class-name"><span class="token operator">&lt;</span><span class="token keyword">double</span><span class="token operator">&gt;</span></span></span><span class="token punctuation">(</span>i<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//整数到浮点数的转换</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><h3 id="dynamic-cast"><a href="#dynamic-cast" class="header-anchor">#</a> dynamic_cast</h3> <ul><li><p>用途：用于在继承关系中进行安全的向下转型（向子类转换）。通常与多态一起使用，可在运行时检查对象的类型。</p></li> <li><p>安全性：在运行时执行，较为安全。如果无法进行安全的向下转型，返回空指针（对于指针）或引发<code>std::bad_cast</code>异常（对于引用）。</p></li></ul> <p>示例：</p> <div class="language-CPP line-numbers-mode"><pre class="language-cpp"><code><span class="token keyword">class</span> <span class="token class-name">Base</span><span class="token punctuation">{</span>
    <span class="token keyword">virtual</span> <span class="token keyword">void</span> <span class="token function">foo</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token punctuation">}</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>
<span class="token keyword">class</span> <span class="token class-name">Derived</span> <span class="token operator">:</span> <span class="token base-clause"><span class="token keyword">public</span> <span class="token class-name">Base</span></span> <span class="token punctuation">{</span>
    <span class="token keyword">void</span> <span class="token function">bar</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token punctuation">}</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>
Base<span class="token operator">*</span> basePtr <span class="token operator">=</span> <span class="token keyword">new</span> Derived<span class="token punctuation">;</span>
Derived derivedPtr <span class="token operator">=</span> <span class="token generic-function"><span class="token function">dynamic_cast</span><span class="token generic class-name"><span class="token operator">&lt;</span>Derived<span class="token operator">*</span><span class="token operator">&gt;</span></span></span><span class="token punctuation">(</span>basePtr<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">if</span><span class="token punctuation">(</span>derivedPtr<span class="token punctuation">)</span><span class="token punctuation">{</span>
    <span class="token comment">//安全的向下转型</span>
    derivedPtr<span class="token operator">-&gt;</span><span class="token function">bar</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br></div></div><h3 id="const-cast"><a href="#const-cast" class="header-anchor">#</a> const_cast</h3> <ul><li>用途：用于添加或去除<code>const</code>限定符。主要用于让常量变量变为非常量，或非常量变为常量。</li> <li>安全性：在编译时执行，较为安全。但要注意，滥用<code>const_cast</code>可能导致未定义行为。</li></ul> <p>示例：</p> <div class="language-CPP line-numbers-mode"><pre class="language-cpp"><code><span class="token keyword">const</span> <span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">42</span><span class="token punctuation">;</span>
<span class="token keyword">int</span><span class="token operator">*</span> nonConstPtr <span class="token operator">=</span> <span class="token generic-function"><span class="token function">const_cast</span><span class="token generic class-name"><span class="token operator">&lt;</span><span class="token keyword">int</span><span class="token operator">*</span><span class="token operator">&gt;</span></span></span><span class="token punctuation">(</span><span class="token operator">&amp;</span>i<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//去除常量限定符</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><h3 id="reinterpret-cast"><a href="#reinterpret-cast" class="header-anchor">#</a> reinterpret_cast</h3> <ul><li>用途：进行低级别的类型转换，通常用于指针和整数之间的转换。不执行类型检查，可能导致未定义行为。</li> <li>安全性：不进行类型检查，非常危险，应慎用。</li></ul> <p>示例：</p> <div class="language-CPP line-numbers-mode"><pre class="language-cpp"><code><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">42</span><span class="token punctuation">;</span>
<span class="token keyword">void</span><span class="token operator">*</span> ptr <span class="token operator">=</span> <span class="token generic-function"><span class="token function">reinterpret_cast</span><span class="token generic class-name"><span class="token operator">&lt;</span><span class="token keyword">void</span><span class="token operator">*</span><span class="token operator">&gt;</span></span></span><span class="token punctuation">(</span><span class="token operator">&amp;</span>i<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//指针到无关类型的转换</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>注意事项：</p> <ul><li><p><code>dynamic_cast</code>主要用于多态场景，只适用于具有虚函数的类，用于安全的向下转型。</p></li> <li><p>滥用强制类型转换可能导致程序错误，建议遵循类型安全和良好的设计原则，尽量减少类型转换的需求。</p></li></ul> <h2 id="_9、vector数组迭代器失效时机-如何使用迭代器删除vector-erase返回值"><a href="#_9、vector数组迭代器失效时机-如何使用迭代器删除vector-erase返回值" class="header-anchor">#</a> 9、Vector数组迭代器失效时机，如何使用迭代器删除vector，erase返回值？</h2> <p>在C++中，<code>std::vector</code>的迭代器失效时机主要涉及到插入和删除操作。迭代器失效是指迭代器不再指向有效的元素或容器末尾，因此应该谨慎操作迭代器。</p> <ol><li>插入元素：如果在<code>std::vector</code>中间插入元素，所有在插入点之后的迭代器都会失效，因为插入操作会导致元素的移动，改变容器的内存布局。</li> <li>删除元素：删除元素时，被删除元素之后的迭代器都会失效。如果使用<code>erase</code>函数删除元素，它会返回指向删除元素之后元素的迭代器。</li> <li>重新分配内存：当<code>std::vector</code>需要重新分配内存以容纳更多元素时，所有迭代器都会失效，因为元素的内存位置已经发生了变化。</li></ol> <p>示例：如何使用<code>erase</code>来删除<code>std::vector</code>中的元素以及<code>erase</code>的返回值</p> <div class="language-CPP line-numbers-mode"><pre class="language-cpp"><code><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;iostream&gt;</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;vector&gt;</span></span>
<span class="token keyword">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    std<span class="token double-colon punctuation">::</span>vector<span class="token operator">&lt;</span><span class="token keyword">int</span><span class="token operator">&gt;</span> numbers <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">,</span><span class="token number">4</span><span class="token punctuation">,</span><span class="token number">5</span><span class="token punctuation">}</span><span class="token punctuation">;</span>
    <span class="token comment">//使用迭代器删除元素</span>
    std<span class="token double-colon punctuation">::</span>vector<span class="token operator">&lt;</span><span class="token keyword">int</span><span class="token operator">&gt;</span><span class="token double-colon punctuation">::</span>iterator it <span class="token operator">=</span> numbers<span class="token punctuation">.</span><span class="token function">begin</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">+</span><span class="token number">2</span><span class="token punctuation">;</span><span class="token comment">//指向元素3</span>
    it <span class="token operator">=</span> numbers<span class="token punctuation">.</span><span class="token function">erase</span><span class="token punctuation">(</span>it<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//删除元素3，it指向元素4</span>
    std<span class="token double-colon punctuation">::</span>cout <span class="token operator">&lt;&lt;</span> <span class="token string">&quot;Element at iterator after erasing: &quot;</span> <span class="token operator">&lt;&lt;</span> <span class="token operator">*</span>it <span class="token operator">&lt;&lt;</span> std<span class="token double-colon punctuation">::</span>endl<span class="token punctuation">;</span>
    <span class="token comment">//使用返回值删除元素</span>
    it <span class="token operator">=</span> numbers<span class="token punctuation">.</span><span class="token function">begin</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token number">2</span><span class="token punctuation">;</span><span class="token comment">//指向元素4</span>
    std<span class="token double-colon punctuation">::</span>vector<span class="token operator">&lt;</span><span class="token keyword">int</span><span class="token operator">&gt;</span><span class="token double-colon punctuation">::</span>iterator next_it <span class="token operator">=</span> numbers<span class="token punctuation">.</span><span class="token function">erase</span><span class="token punctuation">(</span>it<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//删除元素4，next_it指向元素5</span>
    std<span class="token double-colon punctuation">::</span>cout <span class="token operator">&lt;&lt;</span> <span class="token string">&quot;Element at iterator after erasing: &quot;</span> <span class="token operator">&lt;&lt;</span> <span class="token operator">*</span>next_it <span class="token operator">&lt;&lt;</span> std<span class="token double-colon punctuation">::</span>endl<span class="token punctuation">;</span>

    <span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> num <span class="token operator">:</span> numbers<span class="token punctuation">)</span><span class="token punctuation">{</span>
        std<span class="token double-colon punctuation">::</span>cout <span class="token operator">&lt;&lt;</span> num <span class="token operator">&lt;&lt;</span> <span class="token string">&quot; &quot;</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    std<span class="token double-colon punctuation">::</span>cout <span class="token operator">&lt;&lt;</span> std<span class="token double-colon punctuation">::</span>endl<span class="token punctuation">;</span>
    <span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br></div></div><p>为了避免迭代器失效，可以采取以下方法：</p> <ol><li>使用索引而不是迭代器：如果可能的话，可以使用索引来访问<code>std::vector</code>的元素，因为索引不会受到插入和删除操作的影响。</li> <li>使用迭代器的稳定操作：C++11引入了一些迭代器操作，如<code>std::vector::emplace</code>，<code>std::vector::emplace_back</code>和<code>std::vector::push_back</code>的重载版本，它们可以用来插入元素而不使迭代器失效。</li></ol> <p>如果需要在<code>std::vector</code>中删除元素，并且要在操作后获得指向下一个元素的有效迭代器，可以使用<code>std::vector::erase</code>函数。这个函数返回指向被删除元素之后元素的迭代器。</p> <h2 id="_10、map数据结构-struct插入map需要注意什么"><a href="#_10、map数据结构-struct插入map需要注意什么" class="header-anchor">#</a> 10、Map数据结构，struct插入map需要注意什么</h2> <p>在C++中，<code>std::map</code>是一个关联容器，用于存储键值对（key-value pairs），并按键的顺序进行排序。</p> <p>你要往<code>std::map</code>中插入<code>struct</code>对象时，需要注意以下几点：</p> <ol><li>定义比较函数或比较运算符：<code>std::map</code>根据键来排序，因此需要确保你的<code>struct</code>类型有定义适当的比较函数或比较运算符（<code>&lt;</code>），以便<code>std::map</code>可以根据键的值进行排序。</li> <li>保证键是唯一的：<code>std::map</code>要求键是唯一的，如果你插入的<code>struct</code>对象具有相同的键，后一个会覆盖前一个。</li> <li>插入操作：使用<code>std::map</code>的<code>insert</code>函数或<code>[]</code>运算符来插入<code>struct</code>对象。</li></ol> <p>下面是一个示例，展示如何将自定义<code>struct</code>对象插入<code>std::map</code>：</p> <div class="language-CPP line-numbers-mode"><pre class="language-cpp"><code><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;iostream&gt;</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;map&gt;</span></span>
<span class="token keyword">using</span> <span class="token keyword">namespace</span> std<span class="token punctuation">;</span>
<span class="token comment">//自定义结构体</span>
<span class="token keyword">struct</span> <span class="token class-name">Person</span><span class="token punctuation">{</span>
    string name<span class="token punctuation">;</span>
    <span class="token keyword">int</span> age<span class="token punctuation">;</span>
    <span class="token comment">//自定义比较函数，根据名称比较</span>
    <span class="token keyword">bool</span> <span class="token keyword">operator</span><span class="token operator">&lt;</span><span class="token punctuation">(</span><span class="token keyword">const</span> Person<span class="token operator">&amp;</span> other<span class="token punctuation">)</span><span class="token keyword">const</span><span class="token punctuation">{</span>
        <span class="token keyword">return</span> name <span class="token operator">&lt;</span> other<span class="token punctuation">.</span>name<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>
<span class="token keyword">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
    map<span class="token operator">&lt;</span>Person<span class="token punctuation">,</span>string<span class="token operator">&gt;</span>personMap<span class="token punctuation">;</span>
    <span class="token comment">//插入数据</span>
    Person person1 <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token string">&quot;Alice&quot;</span><span class="token punctuation">,</span><span class="token number">25</span><span class="token punctuation">}</span><span class="token punctuation">;</span>
    Person person2 <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token string">&quot;Bob&quot;</span><span class="token punctuation">,</span><span class="token number">30</span><span class="token punctuation">}</span><span class="token punctuation">;</span>
    personMap<span class="token punctuation">[</span>person1<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token string">&quot;Entry 1&quot;</span><span class="token punctuation">;</span>
    personMap<span class="token punctuation">[</span>person2<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token string">&quot;Entry 2&quot;</span><span class="token punctuation">;</span>
    
    <span class="token comment">//遍历输出</span>
    <span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">const</span> <span class="token keyword">auto</span><span class="token operator">&amp;</span> entry <span class="token operator">:</span> personMap<span class="token punctuation">)</span><span class="token punctuation">{</span>
        cout <span class="token operator">&lt;&lt;</span> <span class="token string">&quot;Name: &quot;</span> <span class="token operator">&lt;&lt;</span> entry<span class="token punctuation">.</span>first<span class="token punctuation">.</span>name <span class="token operator">&lt;&lt;</span> <span class="token string">&quot;, Age: &quot;</span> <span class="token operator">&lt;&lt;</span> entry<span class="token punctuation">.</span>first<span class="token punctuation">.</span>age <span class="token operator">&lt;&lt;</span> <span class="token string">&quot;, Data: &quot;</span> <span class="token operator">&lt;&lt;</span> entry<span class="token punctuation">.</span>second <span class="token operator">&lt;&lt;</span> endl<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br></div></div><h2 id="_11、进程间通信"><a href="#_11、进程间通信" class="header-anchor">#</a> 11、进程间通信？</h2> <ol><li><p>管道（Pipes）：</p> <ol><li>管道是一种单向通信机制，通常用于父子进程之间或者兄弟进程之间。</li> <li>在Unix/Linux中，可以使用<code>pipe</code>系统调用来创建管道。</li></ol></li> <li><p>命名管道（Named Pipes或FIFO）：</p> <ol><li>命名管道是一种命名的管道，允许不相关的进程进行通信。</li> <li>在Unix/Linux中，可以使用<code>mkfifo</code>函数来创建命名管道。</li></ol></li> <li><p>消息队列（Message Queues）：</p> <ol><li>消息队列是一种可以在进程之间传递数据的通信方式。</li> <li>在Unix/Linux中，可以使用<code>msgget</code>、<code>msgsnd</code>和<code>msgrcv</code>函数来操作消息队列。</li></ol></li> <li><p>共享内存（Shared Memory）：</p> <ol><li><p>共享内存允许多个进程共享同一块物理内存，以便高效地交换数据。</p></li> <li><p>在Unix/Linux中，可以使用<code>shmget</code>、<code>shmat</code>和<code>shmdt</code>函数来操作共享内存。</p> <blockquote><p><code>shmget</code>：用于创建一个新的共享内存段或获取一个现有共享内存段的标识符。</p> <p><code>shmat</code>：用于将共享内存段连接到当前进程的地址空间，以便访问共享内存中的数据。</p> <p><code>shmdt</code>：用于将共享内存段从当前进程的地址空间中分离，使其不再可访问。</p> <p><code>shmctl</code>：用于控制和管理共享内存段，如删除共享内存段等。</p></blockquote></li></ol></li> <li><p>信号（Signals）：</p> <ol><li>信号是一种轻量级的IPC方式，用于通知进程发生了某种事件。</li> <li>信号通常用于处理异步事件，如进程终止或错误发生。</li></ol></li> <li><p>套接字（Sockets）：</p> <ol><li>套接字允许进程在不同的主机上通过网络通信。</li> <li>常见的套接字包括TCP套接字和UDP套接字。</li></ol></li> <li><p>文件锁（File Locking）：</p> <ol><li>文件锁允许进程通过文件系统进行协同工作，确保数据的一致性。</li> <li>文件锁通常用于避免多个进程同时写入相同的文件。</li></ol></li> <li><p>信号量（Semaphores）：</p> <ol><li>信号量是一种用于同步进程之间操作的IPC方式，通常用于解决竞争条件问题。</li> <li>在Unix/Linux中，可以使用<code>semget</code>、<code>semop</code>等函数来操作信号量。</li></ol></li> <li><p>RPC（远程过程调用）：</p> <ol><li>RPC允许进程在不同的机器上调用远程的函数，使得远程调用看起来像本地函数调用。</li></ol></li></ol> <h2 id="_12、如何实现多次运行一个程序只有一个后台进程"><a href="#_12、如何实现多次运行一个程序只有一个后台进程" class="header-anchor">#</a> 12、如何实现多次运行一个程序只有一个后台进程？</h2> <p>要确保一个程序只有一个后台进程在运行，可以使用锁文件（Lock File）的方式来实现。锁文件是一个特殊的文件，用于表示某个进程是否已经在运行。</p> <p>举个例子：</p> <ol><li>创建一个锁文件：在程序启动时，检查是否存在一个特定的锁文件。如果锁文件不存在，程序可以创建一个锁文件，并继续执行。如果锁文件已经存在，说明另一个实例正在运行，程序应该退出。</li> <li>运行程序：程序在创建锁文件后，继续执行正常的任务。</li> <li>删除锁文件：当程序完成任务后，应该删除锁文件，以允许将来的实例运行。</li></ol> <div class="language-CPP line-numbers-mode"><pre class="language-cpp"><code><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;iostream&gt;</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;fstream&gt;</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;cstdlib&gt;</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;thread&gt;</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;chrono&gt;</span></span>

<span class="token keyword">bool</span> <span class="token function">isAnotherInstanceRunning</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
    <span class="token comment">//尝试打开锁文件</span>
    std<span class="token double-colon punctuation">::</span>ifstream <span class="token function">lockFile</span><span class="token punctuation">(</span><span class="token string">&quot;myapp.lock&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">if</span><span class="token punctuation">(</span>lockFile<span class="token punctuation">.</span><span class="token function">is_open</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
        <span class="token comment">//锁文件存在，另一个实例正在运行</span>
        lockFile<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">return</span> <span class="token boolean">true</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token comment">//锁文件不存在，当前实例可以运行</span>
    std<span class="token double-colon punctuation">::</span>ofstream <span class="token function">newLockFile</span><span class="token punctuation">(</span><span class="token string">&quot;myapp.lock&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">return</span> <span class="token boolean">false</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token keyword">void</span> <span class="token function">removeLockFile</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
    std<span class="token double-colon punctuation">::</span><span class="token function">remove</span><span class="token punctuation">(</span><span class="token string">&quot;myapp.lock&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token keyword">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
    <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token function">isAnotherInstanceRunning</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
        std<span class="token double-colon punctuation">::</span>cout <span class="token operator">&lt;&lt;</span> <span class="token string">&quot;Another instance is already running. Exiting.&quot;</span> <span class="token operator">&lt;&lt;</span> std<span class="token double-colon punctuation">::</span>endl<span class="token punctuation">;</span>
        <span class="token keyword">return</span> <span class="token number">1</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token comment">// 正常的应用逻辑</span>
    std<span class="token double-colon punctuation">::</span>cout <span class="token operator">&lt;&lt;</span> <span class="token string">&quot;Running the application...&quot;</span> <span class="token operator">&lt;&lt;</span> std<span class="token double-colon punctuation">::</span>endl<span class="token punctuation">;</span>
    <span class="token comment">// 模拟应用程序的工作</span>
    std<span class="token double-colon punctuation">::</span>this_thread<span class="token double-colon punctuation">::</span><span class="token function">sleep_for</span><span class="token punctuation">(</span>std<span class="token double-colon punctuation">::</span>chrono<span class="token double-colon punctuation">::</span><span class="token function">seconds</span><span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment">// 删除锁文件，允许其他实例运行</span>
    <span class="token function">removeLockFile</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br></div></div><p>程序首先检查是否存在名为<code>myapp.lock</code>的锁文件。如果锁文件存在，程序会发出警告并退出。如果锁文件不存在，程序创建锁文件并执行其正常任务。在任务完成后，程序会删除锁文件，以允许将来的实例运行。</p> <h2 id="_13、tcp三次握手为什么不是2次或者4次"><a href="#_13、tcp三次握手为什么不是2次或者4次" class="header-anchor">#</a> 13、Tcp三次握手为什么不是2次或者4次？</h2> <p>TCP（传输控制协议）使用三次握手建立连接的原因是为了确保可靠性和防止旧的连接请求被误认为是新的连接请求。</p> <p><strong>这三次握手的目的是：</strong></p> <ol><li>同步双方的序列号（Sequence Number）：在TCP连接建立期间，双方需要交换初始的序列号，以确保数据包按正确的顺序传递。三次握手允许双方同步他们的初始序列号。</li> <li>确保可靠连接：第三次握手是客户端向服务器发送一个确认，表示服务器已经知道客户端的初始序列号，这样确保了双方都知道对方已经准备好建立连接。</li> <li>防止旧连接的重新连接：假设连接的第三次握手被延迟，客户端可能认为连接失败并尝试重新连接。如果连接只采用两次握手，这个延迟的第三次握手可能被错误地解释为新连接的请求，从而导致连接混乱。</li></ol> <p><strong>下面是三次握手的详细步骤：</strong></p> <ol><li>客户端向服务器发送连接请求：客户端发送一个TCP数据包，其中包含SYN标志位，表示客户端希望建立连接。同时，客户端会选择一个随机的初始序列号。</li> <li>服务器接受连接请求并回应：服务器接受客户端的连接请求，并发送回一个TCP数据包，其中包含SYN和ACK标志位。服务器也会选择一个随机的初始序列号。</li> <li>客户端确认连接：客户端接受服务器的回应，发送一个带有ACK标志位的数据包，表示连接已建立。此时，客户端和服务器都知道了彼此的初始序列号，连接已经准备好使用。</li></ol> <p>四次握手不是必需的，因为第四次握手在连接建立后通常没有必要。连接的终止通常需要四次挥手，因为双方都需要确认数据传输已经完成，然后才能安全地关闭连接。但在连接建立过程中，三次握手足以确保连接的可靠性和唯一性。</p> <h2 id="_14、二叉树最大深度"><a href="#_14、二叉树最大深度" class="header-anchor">#</a> 14、二叉树最大深度？</h2> <p>给定一个二叉树 <code>root</code> ，返回其最大深度。</p> <p>二叉树的 最大深度 是指从根节点到最远叶子节点的最长路径上的节点数。</p> <h3 id="思路-双栈法"><a href="#思路-双栈法" class="header-anchor">#</a> 思路：双栈法</h3> <p>使用两个栈，一个用于存储节点，另一个用于存储节点的深度。我们从根节点开始，逐层遍历树的节点，同时记录每个节点的深度。最终，返回最大深度作为结果。这种方法避免了递归，减小了函数调用栈的开销。</p> <div class="language-CPP line-numbers-mode"><pre class="language-cpp"><code><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;iostream&gt;</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;stack&gt;</span></span>
<span class="token comment">//定义二叉树节点</span>
<span class="token keyword">struct</span> <span class="token class-name">TreeNode</span> <span class="token punctuation">{</span>
    <span class="token keyword">int</span> val<span class="token punctuation">;</span>
    TreeNode <span class="token operator">*</span>left<span class="token punctuation">;</span>
    TreeNode <span class="token operator">*</span>right<span class="token punctuation">;</span>
    <span class="token function">TreeNode</span><span class="token punctuation">(</span><span class="token keyword">int</span> x<span class="token punctuation">)</span><span class="token operator">:</span><span class="token function">val</span><span class="token punctuation">(</span>x<span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token function">left</span><span class="token punctuation">(</span><span class="token keyword">nullptr</span><span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token function">right</span><span class="token punctuation">(</span><span class="token keyword">nullptr</span><span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token punctuation">}</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>
<span class="token keyword">class</span> <span class="token class-name">Solution</span><span class="token punctuation">{</span>
    <span class="token keyword">public</span><span class="token operator">:</span>
    <span class="token keyword">int</span> <span class="token function">maxDepth</span><span class="token punctuation">(</span>TreeNode <span class="token operator">*</span>root<span class="token punctuation">)</span><span class="token punctuation">{</span>
        <span class="token keyword">if</span><span class="token punctuation">(</span>root <span class="token operator">==</span> <span class="token keyword">nullptr</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span><span class="token comment">//如果根节点为空，返回深度0</span>
        <span class="token punctuation">}</span> 
        <span class="token comment">//使用DFS进行深度搜索</span>
        std<span class="token double-colon punctuation">::</span>stack<span class="token operator">&lt;</span>TreeNode<span class="token operator">*</span><span class="token operator">&gt;</span> nodes<span class="token punctuation">;</span>
        std<span class="token double-colon punctuation">::</span>stack<span class="token operator">&lt;</span><span class="token keyword">int</span><span class="token operator">&gt;</span> depths<span class="token punctuation">;</span>
        <span class="token keyword">int</span> max <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
        nodes<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>root<span class="token punctuation">)</span><span class="token punctuation">;</span>
        depths<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//根节点的深度为1</span>
        <span class="token keyword">while</span><span class="token punctuation">(</span><span class="token operator">!</span>nodes<span class="token punctuation">.</span><span class="token function">empty</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
            TreeNode<span class="token operator">*</span> node <span class="token operator">=</span> nodes<span class="token punctuation">.</span><span class="token function">top</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            nodes<span class="token punctuation">.</span><span class="token function">pop</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token keyword">int</span> depth <span class="token operator">=</span> depths<span class="token punctuation">.</span><span class="token function">top</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            depths<span class="token punctuation">.</span><span class="token function">pop</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            max <span class="token operator">=</span> std<span class="token double-colon punctuation">::</span><span class="token function">max</span><span class="token punctuation">(</span>max<span class="token punctuation">,</span>depth<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//更新最大深度</span>
            <span class="token comment">//遍历左子树</span>
            <span class="token keyword">if</span><span class="token punctuation">(</span>node<span class="token operator">-&gt;</span>left<span class="token punctuation">)</span><span class="token punctuation">{</span>
                nodes<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>node<span class="token operator">-&gt;</span>left<span class="token punctuation">)</span><span class="token punctuation">;</span>
                depths<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>depth<span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//左子树深度加1</span>
            <span class="token punctuation">}</span>
            <span class="token comment">//遍历右子树</span>
            <span class="token keyword">if</span><span class="token punctuation">(</span>node<span class="token operator">-&gt;</span>right<span class="token punctuation">)</span><span class="token punctuation">{</span>
                nodes<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>node<span class="token operator">-&gt;</span>right<span class="token punctuation">)</span><span class="token punctuation">;</span>
                depths<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>depth<span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//右子树深度加1</span>
            <span class="token punctuation">}</span>
        <span class="token punctuation">}</span>
        <span class="token keyword">return</span> max<span class="token punctuation">;</span><span class="token comment">//返回最大深度</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>

<span class="token keyword">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
    <span class="token comment">//创建一个二叉树</span>
    TreeNode<span class="token operator">*</span> root <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token function">TreeNode</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    root<span class="token operator">-&gt;</span>left <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token function">TreeNode</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    root<span class="token operator">-&gt;</span>right <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token function">TreeNode</span><span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    root<span class="token operator">-&gt;</span>left<span class="token operator">-&gt;</span>left <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token function">TreeNode</span><span class="token punctuation">(</span><span class="token number">4</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    root<span class="token operator">-&gt;</span>left<span class="token operator">-&gt;</span>right <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token function">TreeNode</span><span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token comment">//创建Solution对象</span>
    Solution solution<span class="token punctuation">;</span>
    <span class="token comment">//计算二叉树的最大深度</span>
    <span class="token keyword">int</span> depth <span class="token operator">=</span> solution<span class="token punctuation">.</span><span class="token function">maxDepth</span><span class="token punctuation">(</span>root<span class="token punctuation">)</span><span class="token punctuation">;</span>
    std<span class="token double-colon punctuation">::</span>cout <span class="token operator">&lt;&lt;</span> <span class="token string">&quot;二叉树的最大深度为：&quot;</span> <span class="token operator">&lt;&lt;</span> depth <span class="token operator">&lt;&lt;</span> std<span class="token double-colon punctuation">::</span>endl<span class="token punctuation">;</span>
    <span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br><span class="line-number">37</span><br><span class="line-number">38</span><br><span class="line-number">39</span><br><span class="line-number">40</span><br><span class="line-number">41</span><br><span class="line-number">42</span><br><span class="line-number">43</span><br><span class="line-number">44</span><br><span class="line-number">45</span><br><span class="line-number">46</span><br><span class="line-number">47</span><br><span class="line-number">48</span><br><span class="line-number">49</span><br><span class="line-number">50</span><br><span class="line-number">51</span><br><span class="line-number">52</span><br><span class="line-number">53</span><br><span class="line-number">54</span><br><span class="line-number">55</span><br><span class="line-number">56</span><br><span class="line-number">57</span><br></div></div><h3 id="思路-递归"><a href="#思路-递归" class="header-anchor">#</a> 思路：递归</h3> <ol><li>如果二叉树为空（即根节点为空），则深度为0。</li> <li>否则，分别计算左子树和右子树的最大深度。</li> <li>最大深度为左子树的最大深度和右子树的最大深度的较大值加1。</li> <li>递归地应用这个规则，直到到达叶子节点。</li></ol> <p>这个算法通过递归遍历整棵树，每次调用递归函数时，都将问题缩小到子树上，最终得出树的最大深度。这是一种自顶向下的方法，它从根节点开始，逐层深入树的结构。</p> <div class="language-CPP line-numbers-mode"><pre class="language-cpp"><code><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;iostream&gt;</span></span>
<span class="token comment">// 二叉树的节点定义</span>
<span class="token keyword">struct</span> <span class="token class-name">TreeNode</span> <span class="token punctuation">{</span>
    <span class="token keyword">int</span> val<span class="token punctuation">;</span>
    TreeNode<span class="token operator">*</span> left<span class="token punctuation">;</span>
    TreeNode<span class="token operator">*</span> right<span class="token punctuation">;</span>
    <span class="token function">TreeNode</span><span class="token punctuation">(</span><span class="token keyword">int</span> x<span class="token punctuation">)</span><span class="token operator">:</span><span class="token function">val</span><span class="token punctuation">(</span>x<span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token function">left</span><span class="token punctuation">(</span><span class="token keyword">nullptr</span><span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token function">right</span><span class="token punctuation">(</span><span class="token keyword">nullptr</span><span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token punctuation">}</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>

<span class="token keyword">int</span> <span class="token function">maxDepth</span><span class="token punctuation">(</span>TreeNode<span class="token operator">*</span> root<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token comment">// 如果树为空，深度为0</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>root <span class="token operator">==</span> <span class="token keyword">nullptr</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token comment">// 递归计算左子树和右子树的深度</span>
    <span class="token keyword">int</span> leftDepth <span class="token operator">=</span> <span class="token function">maxDepth</span><span class="token punctuation">(</span>root<span class="token operator">-&gt;</span>left<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">int</span> rightDepth <span class="token operator">=</span> <span class="token function">maxDepth</span><span class="token punctuation">(</span>root<span class="token operator">-&gt;</span>right<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment">// 返回左子树和右子树深度的较大值加1</span>
    <span class="token keyword">return</span> std<span class="token double-colon punctuation">::</span><span class="token function">max</span><span class="token punctuation">(</span>leftDepth<span class="token punctuation">,</span> rightDepth<span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token keyword">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token comment">// 创建一个示例二叉树</span>
    TreeNode<span class="token operator">*</span> root    <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token function">TreeNode</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    root<span class="token operator">-&gt;</span>left 		  <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token function">TreeNode</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    root<span class="token operator">-&gt;</span>right 	  <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token function">TreeNode</span><span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    root<span class="token operator">-&gt;</span>left<span class="token operator">-&gt;</span>left  <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token function">TreeNode</span><span class="token punctuation">(</span><span class="token number">4</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    root<span class="token operator">-&gt;</span>left<span class="token operator">-&gt;</span>right <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token function">TreeNode</span><span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment">// 计算最大深度</span>
    <span class="token keyword">int</span> depth <span class="token operator">=</span> <span class="token function">maxDepth</span><span class="token punctuation">(</span>root<span class="token punctuation">)</span><span class="token punctuation">;</span>
    std<span class="token double-colon punctuation">::</span>cout <span class="token operator">&lt;&lt;</span> <span class="token string">&quot;二叉树的最大深度为: &quot;</span> <span class="token operator">&lt;&lt;</span> depth <span class="token operator">&lt;&lt;</span> std<span class="token double-colon punctuation">::</span>endl<span class="token punctuation">;</span>
    <span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br></div></div><h2 id="_15、反转链表"><a href="#_15、反转链表" class="header-anchor">#</a> 15、反转链表？</h2> <p>思路：</p> <ol><li>创建两个指针，<code>prev</code>（前一个节点）和<code>current</code>（当前节点），并初始化为<code>nullptr</code>和链表的头节点，即<code>head</code>。</li> <li>使用一个<code>while</code>循环来迭代整个链表，循环条件是<code>current</code>不为<code>nullptr</code>。在每一次迭代中：
<ol><li>保存<code>current</code>的下一个节点为<code>next</code>，以免在反转指针后丢失对后续节点的引用。</li> <li>将<code>current</code>的<code>next</code>指针指向<code>prev</code>，从而反转当前节点。</li> <li>更新<code>prev</code>为<code>current</code>，将<code>current</code>前进到下一个节点，即<code>next</code>。</li></ol></li> <li>当循环结束时，<code>prev</code>将指向反转后链表的头节点，而<code>current</code>将为<code>nullptr</code>。</li> <li>返回<code>prev</code>，作为新链表的头节点。</li></ol> <div class="language-CPP line-numbers-mode"><pre class="language-cpp"><code><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;iostream&gt;</span></span>
<span class="token comment">//定义链表节点结构</span>
<span class="token keyword">struct</span> <span class="token class-name">ListNode</span> <span class="token punctuation">{</span>
    <span class="token keyword">int</span> val<span class="token punctuation">;</span>
    ListNode<span class="token operator">*</span> next<span class="token punctuation">;</span>
    <span class="token function">ListNode</span><span class="token punctuation">(</span><span class="token keyword">int</span> x<span class="token punctuation">)</span><span class="token operator">:</span><span class="token function">val</span><span class="token punctuation">(</span>x<span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token function">next</span><span class="token punctuation">(</span><span class="token keyword">nullptr</span><span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token punctuation">}</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>
<span class="token keyword">class</span> <span class="token class-name">Solution</span><span class="token punctuation">{</span>
<span class="token keyword">public</span><span class="token operator">:</span>
    ListNode<span class="token operator">*</span> <span class="token function">reverseList</span><span class="token punctuation">(</span>ListNode<span class="token operator">*</span> head<span class="token punctuation">)</span><span class="token punctuation">{</span>
        ListNode<span class="token operator">*</span> prev <span class="token operator">=</span> <span class="token keyword">nullptr</span><span class="token punctuation">;</span><span class="token comment">//前一个节点初始化为nullptr</span>
        ListNode<span class="token operator">*</span> current <span class="token operator">=</span> head<span class="token punctuation">;</span><span class="token comment">//当前节点从链表头开始</span>
        <span class="token keyword">while</span><span class="token punctuation">(</span>current<span class="token punctuation">)</span><span class="token punctuation">{</span>
            ListNode<span class="token operator">*</span> next <span class="token operator">=</span> current<span class="token operator">-&gt;</span>next<span class="token punctuation">;</span><span class="token comment">//保存下一个节点</span>
            current<span class="token operator">-&gt;</span>next <span class="token operator">=</span> prev<span class="token punctuation">;</span><span class="token comment">//当前节点指向前一个节点完成反转</span>
            prev <span class="token operator">=</span> current<span class="token punctuation">;</span><span class="token comment">//更新前一个节点位当前节点</span>
            current <span class="token operator">=</span> next<span class="token punctuation">;</span><span class="token comment">//当前节点指向下一个节点，继续迭代</span>
        <span class="token punctuation">}</span>
        <span class="token keyword">return</span> prev<span class="token punctuation">;</span><span class="token comment">//prev最终指向新链表的头节点</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>
<span class="token keyword">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
    <span class="token comment">//创建一个链表：1-》2-》3-》4-》5</span>
    ListNode<span class="token operator">*</span> head <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token function">ListNode</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    head<span class="token operator">-&gt;</span>next <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token function">ListNode</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    head<span class="token operator">-&gt;</span>next<span class="token operator">-&gt;</span>next <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token function">ListNode</span><span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    head<span class="token operator">-&gt;</span>next<span class="token operator">-&gt;</span>next<span class="token operator">-&gt;</span>next <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token function">ListNode</span><span class="token punctuation">(</span><span class="token number">4</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    head<span class="token operator">-&gt;</span>next<span class="token operator">-&gt;</span>next<span class="token operator">-&gt;</span>next<span class="token operator">-&gt;</span>next <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token function">ListNode</span><span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    
    <span class="token comment">//创建Solution对象</span>
    Solution solution<span class="token punctuation">;</span>
    <span class="token comment">//反转链表</span>
    ListNode<span class="token operator">*</span> reversedHead <span class="token operator">=</span> solution<span class="token punctuation">.</span><span class="token function">reverseList</span><span class="token punctuation">(</span>head<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment">//打印反转后的链表</span>
    ListNode<span class="token operator">*</span> current <span class="token operator">=</span> reversedHead<span class="token punctuation">;</span>
    <span class="token keyword">while</span><span class="token punctuation">(</span>current<span class="token punctuation">)</span><span class="token punctuation">{</span>
        std<span class="token double-colon punctuation">::</span>cout <span class="token operator">&lt;&lt;</span> current<span class="token operator">-&gt;</span>val <span class="token operator">&lt;&lt;</span> <span class="token string">&quot; -&gt; &quot;</span><span class="token punctuation">;</span>
        current <span class="token operator">=</span> current<span class="token operator">-&gt;</span>next<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    std<span class="token double-colon punctuation">::</span>cout <span class="token operator">&lt;&lt;</span> <span class="token string">&quot;nullptr&quot;</span> <span class="token operator">&lt;&lt;</span> std<span class="token double-colon punctuation">::</span>endl<span class="token punctuation">;</span>
    <span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br><span class="line-number">37</span><br><span class="line-number">38</span><br><span class="line-number">39</span><br><span class="line-number">40</span><br><span class="line-number">41</span><br><span class="line-number">42</span><br></div></div><p>参考文章</p> <p>https://mp.weixin.qq.com/s/6B5wKMy2vBW9RfomiEFeqQ</p></div></div> <!----> <div class="page-edit"><!----> <!----> <div class="last-updated"><span class="prefix">上次更新:</span> <span class="time">2024/6/3 14:54:44</span></div></div> <div class="page-nav-wapper"><div class="page-nav-centre-wrap"><a href="/pages/d69946/" class="page-nav-centre page-nav-centre-prev"><div class="tooltip">虎牙C++技术面经</div></a> <a href="/pages/c7c01f/" class="page-nav-centre page-nav-centre-next"><div class="tooltip">完美世界秋招 C++ 游戏开发面经（Cpp部分）</div></a></div> <div class="page-nav"><p class="inner"><span class="prev">
        ←
        <a href="/pages/d69946/" class="prev">虎牙C++技术面经</a></span> <span class="next"><a href="/pages/c7c01f/">完美世界秋招 C++ 游戏开发面经（Cpp部分）</a>→
      </span></p></div></div></div> <!----></main></div> <div class="footer"><!----> 
  Theme by
  <a href="https://github.com/xugaoyi/vuepress-theme-vdoing" target="_blank" title="本站主题">Vdoing</a> 
    | Copyright © 2023-2025
    <span>霜晨月</span></div> <div class="buttons"><div title="返回顶部" class="button blur go-to-top iconfont icon-fanhuidingbu" style="display:none;"></div> <div title="去评论" class="button blur go-to-comment iconfont icon-pinglun" style="display:none;"></div> <div title="主题模式" class="button blur theme-mode-but iconfont icon-zhuti"><ul class="select-box" style="display:none;"><li class="iconfont icon-zidong">
          跟随系统
        </li><li class="iconfont icon-rijianmoshi">
          浅色模式
        </li><li class="iconfont icon-yejianmoshi">
          深色模式
        </li><li class="iconfont icon-yuedu">
          阅读模式
        </li></ul></div></div> <!----> <!----> <!----></div><div class="global-ui"><canvas id="vuepress-canvas-cursor"></canvas></div></div>
    <script src="/assets/js/app.67adcfd9.js" defer></script><script src="/assets/js/4.9aaa1650.js" defer></script><script src="/assets/js/1.5474518c.js" defer></script><script src="/assets/js/3.593d14fc.js" defer></script><script src="/assets/js/192.712a164e.js" defer></script>
  </body>
</html>
